package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 功能描述: 员工登录
     * @param request
     * @param employee
     * @return com.itheima.reggie.common.R<com.itheima.reggie.entity.Employee>
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        log.info(employee.toString());

        // 1、将页面提交的密码进行MD5计算
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());

        // 2、用页面提交的用户名去数据库查询是否存在
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(lqw);

        // 3、如果查询不到直接返回用户不存在
        if (emp == null) {
            return R.error("用户不存在");
        }

        // 4、对比密码是否一致，不一致则返回密码错误
        if (!emp.getPassword().equals(password)) {
            return R.error("密码错误");
        }

        // 5、判断用户状态，禁用则返回已禁用
        if (emp.getStatus() == 0){
            return R.error("账户已禁用");
        }
        // 6、登录成功，将员工id存入Session并返回登录成功结果
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 功能描述:    用户退出
     * @param request
     * @return com.itheima.reggie.common.R<java.lang.String>
     */
    @PostMapping("logout")
    public R<String> logout(HttpServletRequest request){

        log.info("用户退出id:{}", request.getSession().getAttribute("employee"));
        // 清理session中保存的当前登录用户的id
        request.getSession().removeAttribute("employee");
//        request.getSession().invalidate();
        return R.success("退出成功");

    }

    /**
     * 功能描述: 新增员工
     * @param employee
     * @return com.itheima.reggie.common.R<java.lang.String>
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee){
        log.info("新增员工:{}", employee);
        // 设置统一的默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);

        return R.success("新增员工成功");
    }

    /**
     * 功能描述: 员工信息分类
     * @param page
     * @param pageSize
     * @param name
     * @return com.itheima.reggie.common.R<com.itheima.reggie.entity.Employee>
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        // 创建分页构造器
        Page<Employee> pageInfo = new Page<>(page, pageSize);

        // 创建条件构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        lqw.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo, lqw);

        return R.success(pageInfo);
    }

    /**
     * 功能描述: 根据id修改员工信息
     * @param employee
     * @return com.itheima.reggie.common.R<java.lang.String>
     */
    @PutMapping
    public R<String> updateById(@RequestBody Employee employee) {
        log.info("修改员工信息{}", employee.toString());

        employeeService.updateById(employee);

        return R.success("修改成功");
    }

    /**
     * 功能描述: 根据id查询员工信息
     * @param id
     * @return com.itheima.reggie.common.R<com.itheima.reggie.entity.Employee>
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){

        Employee employee = employeeService.getById(id);
        if (employee != null) {
            return R.success(employee);
        }
        return R.error("没有查询到对应员工信息");
    }





}
